/*******************************************************************************
MPLAB Harmony Application Source File

Company:
Microchip Technology Inc.

File Name:
app.c

Summary:
This file contains the source code for the MPLAB Harmony application.

Description:
This file contains the source code for the MPLAB Harmony application. It
implements the logic of the application's state machine and it may call
API routines of other MPLAB Harmony modules in the system, such as drivers,
system services, and middleware. However, it does not call any of the
system interfaces (such as the "Initialize" and "Tasks" functions) of any of
the modules in the system or make any assumptions about when those functions
are called. That is the responsibility of the configuration-specific system
files.
*******************************************************************************/

// DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2013-2014 released Microchip Technology Inc. All rights reserved.

Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).

You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.

SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
// DOM-IGNORE-END


// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************

#include "app.h"

DRV_HANDLE handleTmr1; //タイマ1ハンドル
DRV_HANDLE handleTmr2; //タイマ2ハンドル

int LED;
int LED2;

int Count; //カウンタ



void Tmr1ISR(uintptr_t context, uint32_t alarmCount ) //タイマ1 コールバック関数
{
Count++;
if(Count >= 100) //1000msec 経過
{
Count = 0;
if(LED == 0)
{
LED = 1;
LATGbits.LATG15 = 1; //LED ON
}
else
{
LED = 0;
LATGbits.LATG15 = 0; //LED OFF
}
}
}



void Tmr2ISR(uintptr_t context, uint32_t alarmCount ) //タイマ2 コールバック関数
{
if(LED2 == 0)
{
LED2 = 1;
LATDbits.LATD4 = 1;
LATAbits.LATA15 = 1;
LATAbits.LATA14 = 1;
}
else
{
LED2 = 0;
LATDbits.LATD4 = 0;
LATAbits.LATA15 = 0;
LATAbits.LATA14 = 0;
}
}








// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************

// *****************************************************************************
/* Application Data

Summary:
Holds application data

Description:
This structure holds the application's data.

Remarks:
This structure should be initialized by the APP_Initialize function.

Application strings and buffers are be defined outside this structure.
*/

APP_DATA appData;

// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************
// *****************************************************************************

/* TODO: Add any necessary callback functions.
*/

// *****************************************************************************
// *****************************************************************************
// Section: Application Local Functions
// *****************************************************************************
// *****************************************************************************


/* TODO: Add any necessary local functions.
*/


// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************

/*******************************************************************************
Function:
void APP_Initialize ( void )

Remarks:
See prototype in app.h.
*/

void APP_Initialize ( void )
{
/* Place the App state machine in its initial state. */
appData.state = APP_STATE_INIT;


/* TODO: Initialize your application's state machine and other
* parameters.
*/



//タイマ1 コールバック(割り込み)諸元設定
handleTmr1 = DRV_TMR_Open( DRV_TMR_INDEX_0, DRV_IO_INTENT_EXCLUSIVE ); //タイマハンドル取得

uint32_t divider1 = 3906; //周期レジスタ初期値 //最初の割り込みまでの時間をセット
//5 nsec x2 x 3906 x 256 = 9,999,360 nsec = 10msec

DRV_TMR_AlarmRegister ( handleTmr1, divider1, true, 0, Tmr1ISR ); //繰り返し呼び出し
//コールバック関数繰り返し呼び出しをセットアップ
// bool DRV_TMR_AlarmRegister(
// DRV_HANDLE handle,
// uint32_t divider, //周期レジスタ初期値
// bool isPeriodic, //周期性の有無
// uintptr_t context,
// DRV_TMR_CALLBACK callBack
// );

DRV_TMR_Start(handleTmr1); //タイマ1割り込み許可


//タイマ2 コールバック(割り込み)諸元設定
handleTmr2 = DRV_TMR_Open ( DRV_TMR_INDEX_1, DRV_IO_INTENT_EXCLUSIVE );//タイマハンドル取得

uint32_t divider2 = 31250; //周期レジスタ初期値 //最初の割り込みまでの時間をセット
//5 nsec x2 x 31250 x 64 = 20,000,000 nsec = 20 msec

DRV_TMR_AlarmRegister ( handleTmr2, divider2, true, 0, Tmr2ISR ); //繰り返し呼び出し
//コールバック関数繰り返し呼び出しをセットアップ
// bool DRV_TMR_AlarmRegister(
// DRV_HANDLE handle,
// uint32_t divider, //周期レジスタ初期値
// bool isPeriodic, //周期性の有無
// uintptr_t context,
// DRV_TMR_CALLBACK callBack
// );


//DRV_TMR_AlarmEnable(handleTmr2,true);

DRV_TMR_Start(handleTmr2); //タイマ2割り込み許可

}


/******************************************************************************
Function:
void APP_Tasks ( void )

Remarks:
See prototype in app.h.
*/

void APP_Tasks ( void )
{

/* Check the application's current state. */
switch ( appData.state )
{
/* Application's initial state. */
case APP_STATE_INIT:
{
bool appInitialized = true;


if (appInitialized)
{

appData.state = APP_STATE_SERVICE_TASKS;
}
break;
}

case APP_STATE_SERVICE_TASKS:
{

break;
}

/* TODO: implement your application state machine.*/


/* The default state should never be executed. */
default:
{
/* TODO: Handle error in application's state machine. */
break;
}
}
}



/*******************************************************************************
End of File
*/